/*
 * @lc app=leetcode.cn id=82 lang=javascript
 *
 * [82] 删除排序链表中的重复元素 II
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function (head) {
  const dummy = new ListNode(0, head);
  let pre = dummy;
  let cur = head;
  while (cur) {
    if (cur.next && cur.val === cur.next.val) {
      let lastDup = cur;
      while (lastDup.next && lastDup.val === lastDup.next.val)
        lastDup = lastDup.next;

      pre.next = lastDup.next;
      cur = lastDup.next;
    } else {
      pre = cur;
      cur = cur.next;
    }
  }
  return dummy.next;
};
// @lc code=end
